<?php

/**
 * 模型对应数据库表 "sys_variate".
 *
 * 以下是可用表sys_variate的栏位:
 * @property string $id
 * @property string $title
 * @property string $value
 */
class SysVariate extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return SysVariate the static model class
     * @author nirsh
     * @example
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'sys_variate';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('title, value', 'required'),
            array('title', 'length', 'max' => 225),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, value', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '编号',
            'title' => '名称',
            'value' => '值',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('value', $this->value, true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }
    /**
      +-----------------------------------------------------
     * 说明 获取变量值
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getVal($id){
        return $this->findByPk($id)->value;
    }
    /**
      +-----------------------------------------------------
     * 说明 设置变量 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getInputValue($data){
        return CHtml::textArea('value', $data->value, array(
            'onchange'=>'saveByAjax(this)',
            'cols'=>'120',
            'rel'=>$data->id,
            'rows'=>'2'
        ));
    }
}